const utils = require('../../utils/util.js');
const app = getApp();

Page({
    data: {
        optionList: [
            {
                icon: ''
            },
            {
                icon: ''
            }
        ],

        showAddBtn: 1,
        showTopMsg:'',

        date: '2018-10-01',
        time: '12:00',
        dateTimeArray: null,
        dateTime: null,
        dateTimeArray1: null,
        dateTime1: null,
        startYear: 2000,
        endYear: 2050,
        param:"",
        id:"",


        voteType: ['单选'],
        // voteType: ['单选', '多选，最多2项', '多选，无限制'],
        voteTypeIndex: 0,

        files: [],
        file_url:''


    },

    onload:function(e){
    },
    formSub:function(e){

        console.log(e);


        var that = this;

        // console.log(that.data.time);

        console.log(that.data.files);
        console.log(that.data.optionList);
        console.log(that.data.optionList[0]['value']);

        var title = e.detail.value.title;
        var content = e.detail.value.content;
        var anonymous = e.detail.value.anonymous;
        // var select = JSON.stringify(that.data.optionList);

        if (title=='' ){

            this.setData({
                showTopTips: true,
                showTopMsg:'标题不能为空'
            });

            setTimeout(function(){
                that.setData({
                    showTopTips: false
                });
            }, 3000);
            return false;
        }

        if (content=='' ){

            this.setData({
                showTopTips: true,
                showTopMsg:'内容不能为空'
            });

            setTimeout(function(){
                that.setData({
                    showTopTips: false
                });
            }, 3000);
            return false;
        }

        if (that.data.optionList.length < 2 ){

            this.setData({
                showTopTips: true,
                showTopMsg:'选项至少要有两个'
            });

            setTimeout(function(){
                that.setData({
                    showTopTips: false
                });
            }, 3000);
            return false;
        }

        if (that.data.optionList[0]['value'] == 'undefined' ||that.data.optionList[0]['value'] == '' || that.data.optionList[1]['value'] == ''){

            this.setData({
                showTopTips: true,
                showTopMsg:'选项内容不能为空'
            });

            setTimeout(function(){
                that.setData({
                    showTopTips: false
                });
            }, 3000);
            return false;
        }

        if (that.data.files.length ==0 ){

            this.setData({
                showTopTips: true,
                showTopMsg:'封面图不能为空'
            });

            setTimeout(function(){
                that.setData({
                    showTopTips: false
                });
            }, 3000);
            return false;
        }






        var i =0;

        var select = '';

        for (i;i<that.data.optionList.length;i++){
            select += that.data.optionList[i]['value']+',';
        }


        console.log(select);
        console.log(that.data.files);



        var end_time = that.data.date + ' ' +that.data.time;
        var cover_url = that.data.file_url;

        var password = e.detail.value.password;

        console.log(select);

        that.data.param = '{"title":"'+title+'","password":"'+password+'","content":"'+content+'","anonymous":"'+anonymous+'","select":"'+select+'","end_time":"'+end_time+'","cover_url":"'+cover_url+'"}';


        let isAjax = true;

        if (isAjax){

            isAjax = false;
            //需要去请求数据
            utils.geturl({
                "handler": "Home",
                "method":"createVote",
                "param": that.data.param,
                'token': app.globalData.token
            }, function (res) {
                console.log(res)
                if (res.code == 200) {
                    wx.showToast({
                        title: '操作成功'
                    });


                } else {
                    wx.showToast({
                        title: '服务器错误,请稍候再试',
                        icon: "none"
                    });
                }
                isAjax = true;
            });
        }



    },
    updateVoteType: function (){
        let _optionList = this.data.optionList;
        let _voteType = this.data.voteType;

        _voteType = [];

        _optionList.map(function (obj, i) {

            if (i === 0){
                _voteType.push('单选');
            }else {
                _voteType.push('多选，最多'+ (i + 1) +'项');
            }

            console.log(i)
            console.log(_voteType)

        })
        _voteType.push('多选，无限制');

        this.setData({voteType: _voteType});
        console.log(111)
    },
    showTopTips: function(e){

        console.log(e);

        var that = this;


        var title = e.detail.value.title;
        var content = e.detail.value.content;
        var select = that.data.optionList;
        var start_time = that.data.time;
        var end_time = that.data.time;

        var title = that.data.title;
        var title = that.data.title;
        var title = that.data.title;
        var title = that.data.title;
        var title = that.data.title;
        var title = that.data.title;


        console.log(that.data.time);



        this.setData({
            showTopTips: true
        });


        setTimeout(function(){
            that.setData({
                showTopTips: false
            });
        }, 3000);
    },
    bindVoteTypeChange: function (e){
        this.setData({
            voteTypeIndex: e.detail.value
        })
    },
    bindTimeChange: function (e) {
        this.setData({
            time: e.detail.value
        })
    },
    bindDateChange: function (e) {
        this.setData({
            date: e.detail.value
        })
    },
    recordValue: function (e){
        let _optionList = this.data.optionList;
        let _index = e.target.dataset.index;
        let value = e.detail.value;
        _optionList[_index].value = value;

        this.setData({optionList: _optionList});

    },
    addOption: function (e){
        let _optionList = this.data.optionList;

        _optionList.push({icon: '/static/img/vote/5.png'})
        this.setData({optionList: _optionList});

        // 选项大于15个后移除添加按钮
        if(_optionList.length >= 15) {
            this.setData({showAddBtn: 0});
        }

        // 更新投票选项
        this.updateVoteType();

    },
    delOption: function (e){
        let _index = e.target.dataset.index;
        let _optionList = this.data.optionList;

        _optionList.splice(_index, 1);

        this.setData({optionList: _optionList});

        // 更新投票选项
        this.updateVoteType();

    },
    chooseImage: function (e) {
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            count: 1, // 最多可以选择的图片张数
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                console.log('图片上传成功啦....');


                console.log(res);


                const tempFilePaths = res.tempFilePaths

                console.log(tempFilePaths[0]);

                wx.uploadFile({
                    url: 'https://zsy.php-coder.cn/?s=Applet/Api/inlet', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        apiversion: 'v.1.0',
                        uuid: 'demo1',
                        nonce: 'demo1',
                        device: 'Applet',
                        handler: 'Api',
                        method: 'appletFileUpload',
                        sign: '',
                    },
                    header: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    success (res){
                        console.log('上传至服务器成功....');

                        const data = JSON.parse(res.data);

                        that.setData({
                            file_url:data.data.obj.img_url,
                            files: that.data.files.concat(tempFilePaths)
                        });
                    }
                });
            }
        })
    },
    previewImage: function(e){
        wx.previewImage({
            current: e.currentTarget.id, // 当前显示图片的http链接
            urls: this.data.files // 需要预览的图片http链接列表
        })
    },
});